// app.js
var QQMapWX = require('./utils/qqmap-wx-jssdk.min.js');
var {
    api,
    setRequestInit
} = require("./utils/api.js");
var util = require("./utils/util.js");
// var baseUrl = "https://www.shmoredental.com";
var baseUrl = "https://testtj1.zrs360.vip";
App({
    globalData: {
        mapKey: 'TUOBZ-7QLL3-ZF73T-RCBW3-EFEMT-JTBHQ',
        userInfo: null,
        baseUrl: baseUrl,
        staticPath: baseUrl
    },
    onLaunch() {
        this.qqmapsdk = new QQMapWX({
            key: this.globalData.mapKey
        });
        setRequestInit(baseUrl);
        this.getSystemInfo();
    },
    getSystemInfo() {
        let {
            statusBarHeight,
            platform
        } = wx.getSystemInfoSync();
        let {
            top,
            height,
            bottom
        } = wx.getMenuButtonBoundingClientRect();
        let capsule = height ? height : 32;
        let navigationBarHeight = statusBarHeight ? statusBarHeight : (platform === 'android' ? 48 : 40);
        if (top && top !== 0 && height && height !== 0) {
            capsule = top + bottom + height;
        }
        wx.setStorageSync('navBarObj', {
            bangsHeight: navigationBarHeight + capsule,
            capsule,
            statusBarHeight,
            menuTop: top
        })
    },
    /**
     * 显示失败提示框
     */
    showError(msg, callback) {
        wx.showModal({
            title: '温馨提示',
            content: msg,
            showCancel: false,
            success: (res) => {
                callback && callback();
            }
        });
    },
    getUsers() {
        return new Promise(resolve => {
            api.getUsers()
                .then(res => {
                    const {
                        data
                    } = res;
                    if (data && data.user) {
                        data.user.coupon_num=data.coupon_num;
                        if(data.user_patient && data.user_patient.id){
                            data.user.user_patient = true;
                            data.user.level_name=data.user_patient.level?data.user_patient.level.level_name:'普通用户';
                        }
                    }
                    resolve({
                        userInfo: util.getUserInfo(data && data.user),
                        integral: data && data.integral ? data.integral : null
                    })

                })
        })
    },
    getKf(phone=null) {
        if(phone){
            wx.showModal({
                title: "欢迎咨询",
                content: "请致电" + phone + "\n(人工服务时间：周一至周日8:00至17：00)",
                confirmText: '拨打电话',
                complete: (result) => {
                    if (result.confirm) {
                        wx.makePhoneCall({
                            phoneNumber: phone
                        }).catch(function () {});
                    }
                }
            })
        }else{
            api.kfPhone().then(res => {
                wx.showModal({
                    title: "欢迎咨询",
                    content: "请致电" + res.data.kefu_phone + "\n(人工服务时间：周一至周日8:00至17：00)",
                    confirmText: '拨打电话',
                    complete: (result) => {
                        if (result.confirm) {
                            wx.makePhoneCall({
                                phoneNumber: res.data.kefu_phone
                            }).catch(function () {});
                        }
                    }
                })
            })
        }
        
    },
    // 金额支付
    moneyWxPlay(payment) {
        return new Promise((resolve, reject) => {
            wx.requestPayment({
                timeStamp: payment.timeStamp, //后端返回的时间戳
                nonceStr: payment.nonceStr, //后端返回的随机字符串
                package: payment.package, //后端返回的prepay_id
                signType: payment.signType, //后端签名算法,根据后端来,后端MD5这里即为MD5
                paySign: payment.paySign, //后端返回的签名
                success(result) {
                    if (result.errMsg == "requestPayment:ok") {
                        wx.showToast({
                            icon: 'success',
                            title: '支付成功'
                        });
                        resolve()
                    } else {
                        wx.showToast({
                            icon: 'none',
                            title: '支付失败'
                        });
                        reject('wxplay')
                    }
                },
                fail(res) {
                    wx.showToast({
                        icon: 'none',
                        title: '支付失败'
                    });
                    reject('wxplay')
                }
            })
        })
    }
})